<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="统计日期" prop="tjrq">
        <el-date-picker  :editable="false"
          clearable
          v-model="queryParams.tjrq"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择统计日期"
        >
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="基地" prop="jd">
        <el-input
          v-model="queryParams.jd"
          placeholder="请输入基地"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报表编号" prop="bbbh">
        <el-input
          v-model="queryParams.bbbh"
          placeholder="请输入报表编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="开始日期" prop="begintime">
        <el-date-picker
          :editable="false"
          clearable
          size="small"
          v-model="queryParams.begintime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择开始日期"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item label="结束日期" prop="endtime">
        <el-date-picker
          :editable="false"
          clearable
          size="small"
          v-model="queryParams.endtime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择结束日期"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <!-- <el-button icon="el-icon-refresh" size="mini" @click="oneKey"
          >生成</el-button
        > -->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['Productionline_itmain:itmain:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['Productionline_itmain:itmain:edit']"
          >修改</el-button
        >
      </el-col> -->

      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="oneKey"
          v-hasPermi="['Productionline_itmain:itmain:add']"
          >生成</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['Productionline_itmain:itmain:remove']"
          >删除</el-button
        >
      </el-col>
      <!--  <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['Productionline_itmain:itmain:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="itmainList"
      @selection-change="handleSelectionChange"
      max-height="550"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="创建日期" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="统计日期" align="center" prop="tjrq" />
      <el-table-column label="基地" align="center" prop="jd" />
      <el-table-column label="报表编号" align="center" prop="bbbh" />
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        min-width="250px"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="excelhandleView(scope.row)"
            >导出</el-button
          >

          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['Productionline_itmain:itmain:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['Productionline_itmain:itmain:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改制程检验原辅料消耗统计对话框 -->

    <el-dialog
      v-myFullscreen
      :title="title"
      :visible.sync="open"
      width="700px"
      append-to-body
      custom-class="myDialog"
      fullscreen
    >
      <TableHeader
        @nameChange="nameChange"
        :data="{
          mainTitle: '制程检验原辅料消耗统计',
          company: '青岛崂山矿泉水有限公司',
          code: form.bbbh,
        }"
      />
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="统计日期" prop="tjrq">
              <el-input
                v-model="form.tjrq"
                placeholder="请输入统计日期"
                :disabled="openDisabled"
              />
              <!--<el-date-picker  :editable="false"
                clearable
                v-model="form.tjrq"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择统计日期"
                :disabled="openDisabled"
              >
              </el-date-picker>-->
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="基地" prop="jd">
              <el-input
                v-model="form.jd"
                placeholder="请输入基地"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="报表编号" prop="bbbh">
              <el-input
                v-model="form.bbbh"
                placeholder="请输入报表编号"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                placeholder="请输入备注"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col> -->
        </el-row>
        <br />
        <el-divider content-position="center"
          >制程检验原辅料消耗统计信息</el-divider
        >
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAddQualityProdSampleItSub"
              :disabled="openDisabled"
              >添加</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="handleDeleteQualityProdSampleItSub"
              :disabled="openDisabled"
              >删除</el-button
            >
          </el-col>
        </el-row>
        <el-table
          :data="qualityProdSampleItSubList"
          :row-class-name="rowQualityProdSampleItSubIndex"
          @selection-change="handleQualityProdSampleItSubSelectionChange"
          ref="qualityProdSampleItSub"
          max-height="550"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column
            label="序号"
            align="center"
            prop="index"
            width="50"
          />
          <el-table-column
            width="200"
            align="center"
            label="报表日期"
            prop="bbrq"
          >
            <template slot-scope="scope">
              <!-- <el-input
                v-model="scope.row.bbrq"
                placeholder="请输入报表日期"
                :disabled="openDisabled"
              /> -->
              <el-date-picker
                :editable="false"
                clearable
                v-model="scope.row.bbrq"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择报表日期"
              >
              </el-date-picker>
            </template>
          </el-table-column>
          <el-table-column
            min-width="230"
            align="center"
            label="产品规格"
            prop="cpgg"
          >
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.cpgg"
                placeholder="请输入产品规格"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column align="center" label="化验取样、成品留样消耗数量">
            <el-table-column width="200" align="center" label="瓶坯" prop="p">
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.p"
                  placeholder="请输入瓶坯"
                  :disabled="openDisabled"
                />
              </template>
            </el-table-column>
            <el-table-column width="200" align="center" label="瓶盖" prop="g">
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.g"
                  placeholder="请输入瓶盖"
                  :disabled="openDisabled"
                />
              </template>
            </el-table-column>
            <el-table-column width="200" align="center" label="标签" prop="bq">
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.bq"
                  placeholder="请输入标签"
                  :disabled="openDisabled"
                />
              </template>
            </el-table-column>
          </el-table-column>
          <el-table-column align="center" label="过程抽检消耗数量">
            <!-- <el-table-column width="200" align="center" label="纸箱" prop="zx">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.zx"
                placeholder="请输入纸箱"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column> -->
            <el-table-column width="200" align="center" label="瓶坯" prop="p2">
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.p2"
                  placeholder="请输入瓶坯"
                  :disabled="openDisabled"
                />
              </template>
            </el-table-column>
            <el-table-column width="200" align="center" label="瓶盖" prop="g2">
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.g2"
                  placeholder="请输入瓶盖"
                  :disabled="openDisabled"
                />
              </template>
            </el-table-column>
            <el-table-column width="200" align="center" label="标签" prop="bq2">
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.bq2"
                  placeholder="请输入标签"
                  :disabled="openDisabled"
                />
              </template>
            </el-table-column>
          </el-table-column>
          <!-- <el-table-column width="200" align="center" label="纸箱" prop="zx2">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.zx2"
                placeholder="请输入纸箱"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column> -->
          <el-table-column align="center" label="单品种消耗数量合计">
            <el-table-column width="200" align="center" label="瓶坯" prop="ph">
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.ph"
                  placeholder="请输入瓶坯"
                  :disabled="openDisabled"
                />
              </template>
            </el-table-column>
            <el-table-column width="200" align="center" label="瓶盖" prop="gh">
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.gh"
                  placeholder="请输入瓶盖"
                  :disabled="openDisabled"
                />
              </template>
            </el-table-column>
            <el-table-column width="200" align="center" label="标签" prop="bqh">
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.bqh"
                  placeholder="请输入标签"
                  :disabled="openDisabled"
                />
              </template>
            </el-table-column>
          </el-table-column>
          <!-- <el-table-column width="200" align="center" label="纸箱" prop="zxh">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.zxh"
                placeholder="请输入纸箱"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column> -->
          <!-- <el-table-column width="200" align="center" label="基地" prop="jd">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.jd"
                placeholder="请输入基地"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column> -->

       <!--   <el-table-column width="200" align="center" label="记录人" prop="jlr">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.jlr"
                placeholder="请输入记录人"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column
            width="200"
            align="center"
            label="备注"
            prop="remark"
          >
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.remark"
                placeholder="请输入备注"
                :disabled="openDisabled"
              />
            </template>   -->
          </el-table-column>
        </el-table>
       <el-row>
                 <el-col :span="6">
                <!--    <el-form-item label="记录人" prop="recorder">
                     <el-input
                       v-model="form.recorder"
                       placeholder="请输入记录人"
                       :disabled="openDisabled"
                     />
                   </el-form-item>    -->
                    <el-form-item label="记录人" prop="recorder">
                                 <!-- <el-input v-model="form.recorder" placeholder="请输入记录人" :disabled="openDisabled" /> -->
                                 <el-select
                                   v-model="form.recorder"
                                   filterable
                                   allow-create
                                   style="width: 100%"
                                   :disabled="openDisabled"
                                 >
                                   <el-option
                                     v-for="item in userList"
                                     :key="item.nickName"
                                     :label="item.nickName"
                                     :value="item.nickName"
                                   ></el-option>
                                 </el-select>
                               </el-form-item>




                 </el-col>
                 <el-col :span="6">
                   <el-form-item label="审核人" prop="reviewer">
                     <UserDialog
                                   @dialogChange="form.reviewer = $event.label"
                                   :echoValue="form.reviewer"
                                   :multiple="false"
                                   :openDisabled="openDisabled"
                                   placeholder="请选择人员"
                                 />
                   </el-form-item>
                 </el-col>
                 <el-col :span="12">
                   <el-form-item label="备注" prop="remark">
                     <el-input
                       v-model="form.remark"
                       placeholder="备注填写"
                       :disabled="openDisabled"
                     />
                   </el-form-item>
                 </el-col>
               </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!openDisabled">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listItmain,
  getItmain,
  delItmain,
  addItmain,
  updateItmain,
  oneKey,
} from "@/api/quality/productionline/itmain";
import { getInfo } from "@/api/login";
import { listUser } from "@/api/system/user";
export default {
  name: "Itmain",
  data() {
    return {
      //弹窗是否禁止编辑
      openDisabled: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedQualityProdSampleItSub: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 制程检验原辅料消耗统计表格数据
      itmainList: [],
      // 制程检验原辅料消耗统计表格数据
      qualityProdSampleItSubList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        tjrq: null,
        jd: null,
        bbbh: null,
        begintime: null,
        endtime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      /* 当前用户名 */
      nickName: "",
      userList: [],
    };
  },
  created() {
    this.getList();
    this.queryUserList();
  },
  methods: {
    oneKey() {
      if (
        this.queryParams.begintime == "" ||
        this.queryParams.endtime == null
      ) {
        this.msgError("请选择开始日期-结束日期");
      } else {
        oneKey(this.queryParams).then((response) => {
          // this.px = response.data;
          // console.log(response.data);
          this.getList();
        });
      }
    },
    /** 查询制程检验原辅料消耗统计列表 */
    getList() {
      this.loading = true;
      listItmain(this.queryParams).then((response) => {
        this.itmainList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tjrq: null,
        jd: null,
        bbbh: "LK-QF-LS1-10-015-11-05",
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        recorder: this.nickName,
        reviewer: null,
      };
      this.qualityProdSampleItSubList = [];
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openDisabled = false;
      this.open = true;
      this.title = "添加制程检验原辅料消耗统计";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.openDisabled = false;
      const id = row.id || this.ids;
      getItmain(id).then((response) => {
        this.form = response.data;
        this.qualityProdSampleItSubList =
          response.data.qualityProdSampleItSubList;
        this.open = true;
        this.title = "修改制程检验原辅料消耗统计";
      });
    },
    /** 查看按钮 */
    handleView(row) {
      this.reset();
      this.openDisabled = true;
      const id = row.id || this.ids;
      getItmain(id).then((response) => {
        this.form = response.data;
        this.qualityProdSampleItSubList =
          response.data.qualityProdSampleItSubList;
        this.open = true;
        this.title = "查看制程检验原辅料消耗统计";
      });
    },

    excelhandleView(row) {
      this.download(
        "Productionline/itmain/exportexcel?id=" + row.id,
        {
          ...this.queryParams,
        },
        "制程检验原辅料消耗统计" + this.parseTime(new Date()) + ".xlsx"
      );
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.qualityProdSampleItSubList =
            this.qualityProdSampleItSubList;
          /**打开遮罩层*/
          const rLoading = this.openLoading();
          if (this.form.id != null) {
            updateItmain(this.form)
              .then((response) => {
                /**成功后执行关闭遮罩层*/
                rLoading.close();
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .catch((error) => {
                /**请求异常后执行关闭遮罩层*/
                rLoading.close();
              });
          } else {
            addItmain(this.form)
              .then((response) => {
                /**成功后执行关闭遮罩层*/
                rLoading.close();
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .catch((error) => {
                /**请求异常后执行关闭遮罩层*/
                rLoading.close();
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除制程检验原辅料消耗统计编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delItmain(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 制程检验原辅料消耗统计序号 */
    rowQualityProdSampleItSubIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 制程检验原辅料消耗统计添加按钮操作 */
    handleAddQualityProdSampleItSub() {
      let obj = {};
      obj.p = "";
      obj.g = "";
      obj.bq = "";
      obj.zx = "";
      obj.p2 = "";
      obj.g2 = "";
      obj.bq2 = "";
      obj.zx2 = "";
      obj.ph = "";
      obj.gh = "";
      obj.bqh = "";
      obj.zxh = "";
      obj.jd = "";
      obj.bbrq = "";
      obj.cpgg = "";
      obj.jlr = "";
      obj.remark = "";
      this.qualityProdSampleItSubList.push(obj);
    },
    /** 制程检验原辅料消耗统计删除按钮操作 */
    handleDeleteQualityProdSampleItSub() {
      if (this.checkedQualityProdSampleItSub.length == 0) {
        this.$alert("请先选择要删除的制程检验原辅料消耗统计数据", "提示", {
          confirmButtonText: "确定",
        });
      } else {
        this.qualityProdSampleItSubList.splice(
          this.checkedQualityProdSampleItSub[0].index - 1,
          1
        );
      }
    },
    /** 单选框选中数据 */
    handleQualityProdSampleItSubSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.qualityProdSampleItSub.clearSelection();
        this.$refs.qualityProdSampleItSub.toggleRowSelection(selection.pop());
      } else {
        this.checkedQualityProdSampleItSub = selection;
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "Productionline_itmain/itmain/export",
        {
          ...this.queryParams,
        },
        "制程检验原辅料消耗统计" + this.parseTime(new Date()) + ".xlsx"
      );
    },
    //接收编号
    nameChange(e) {
      this.form.bbbh = e;
    },


     /* 查询用户列表 */
        queryUserList() {
          listUser().then((response) => {
            this.userList = response.rows;
          });
          getInfo().then((response) => {
            this.nickName = response.user.nickName;
          });
    },
  },
};
</script>
